﻿using Joy.Erp.BasicArchives.Localization;
using System.Linq;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Localization;

namespace Joy.Erp.BasicArchives.Permissions;

public class BasicArchivesPermissionDefinitionProvider : PermissionDefinitionProvider
{
    public override void Define(IPermissionDefinitionContext context)
    {
        var menus = context.GetGroupOrNull(BasicArchivesPermissions.Menus);
        if (menus == null)
        {
            menus = context.AddGroup(BasicArchivesPermissions.Menus, L("Permission:Menus"));
        }

        var administrationMenu = menus.GetPermissionOrNull(BasicArchivesPermissions.AdministrationMenu.Default);
        if (administrationMenu == null)
        {
            administrationMenu = menus.AddPermission(BasicArchivesPermissions.AdministrationMenu.Default, L("Permission:Menus.Administration"));
        }

        var identityManagementMenu = administrationMenu.Children.FirstOrDefault(x => x.Name == BasicArchivesPermissions.AdministrationMenu.IdentityManagement);
        if (identityManagementMenu == null)
        {
            identityManagementMenu = administrationMenu.AddChild(BasicArchivesPermissions.AdministrationMenu.IdentityManagement, L("Permission:Menus.Administration.IdentityManagement"));
        }

        var rolesMenu = identityManagementMenu.Children.FirstOrDefault(x => x.Name == BasicArchivesPermissions.AdministrationMenu.Roles);
        if (rolesMenu == null)
        {
            rolesMenu = identityManagementMenu.AddChild(BasicArchivesPermissions.AdministrationMenu.Roles, L("Permission:Menus.Administration.IdentityManagement.Roles"));
        }

        var usersMenu = identityManagementMenu.Children.FirstOrDefault(x => x.Name == BasicArchivesPermissions.AdministrationMenu.Users);
        if (usersMenu == null)
        {
            usersMenu = identityManagementMenu.AddChild(BasicArchivesPermissions.AdministrationMenu.Users, L("Permission:Menus.Administration.IdentityManagement.Users"));
        }

        var tenantManagementMenu = administrationMenu.Children.FirstOrDefault(x => x.Name == BasicArchivesPermissions.AdministrationMenu.TenantManagement);
        if (tenantManagementMenu == null)
        {
            tenantManagementMenu = administrationMenu.AddChild(BasicArchivesPermissions.AdministrationMenu.TenantManagement, L("Permission:Menus.Administration.TenantManagement"));
        }

        var tenantsMenu = tenantManagementMenu.Children.FirstOrDefault(x => x.Name == BasicArchivesPermissions.AdministrationMenu.Tenants);
        if (tenantsMenu == null)
        {
            tenantsMenu = tenantManagementMenu.AddChild(BasicArchivesPermissions.AdministrationMenu.Tenants, L("Permission:Menus.Administration.TenantManagement.Tenants"));
        }

        var settingsMenu = administrationMenu.Children.FirstOrDefault(x => x.Name == BasicArchivesPermissions.AdministrationMenu.Settings);
        if (settingsMenu == null)
        {
            settingsMenu = administrationMenu.AddChild(BasicArchivesPermissions.AdministrationMenu.Settings, L("Permission:Menus.Administration.Settings"));
        }

        var basicArchivesMenu = menus.GetPermissionOrNull(BasicArchivesPermissions.BasicArchivesMenu.Default);
        if (basicArchivesMenu == null)
        {
            basicArchivesMenu = menus.AddPermission(BasicArchivesPermissions.BasicArchivesMenu.Default, L("Permission:Menus.BasicArchives.ArchivesManagement"));
        }

        var group = context.GetGroupOrNull(BasicArchivesPermissions.GroupName);
        if (group == null)
        {
            group = context.AddGroup(BasicArchivesPermissions.GroupName, L("Permission:BasicArchives"));
        }
    }

    private static LocalizableString L(string name)
    {
        return LocalizableString.Create<BasicArchivesResource>(name);
    }
}
